<template>
    <div class="goodinfo-container">
        <!--尚平图片轮播-->
        <transition
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter">
        <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>
        
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunbotulist="goodImg" :isFull="false"></swiper>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">{{goodInfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:<del>￥{{goodInfo.market_price}}</del>&nbsp;&nbsp;销售价: <span class="now-price">￥{{goodInfo.sell_price}}</span>
                    </p>
                    <p>
                        购买数量：<numbox style="display:inline" @getCount="getSelectCount" :max="goodInfo.stock_quantity"></numbox>
                    </p>
                    <p>
                        <mt-button type="primary" @click="addToShop()">加入购物车</mt-button>
                        <mt-button type="danger">立刻购买</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p >
                        商品货号：{{goodInfo.good_no}}
                    </p>
                    <p>
                       库存量：{{goodInfo.stock_quantity}}
                    </p>
                    <p>
                        上架时间：{{goodInfo.add_time | dateForm}}
                    </p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button plain size="large" type="primary" @click="goDesc(id)">图文介绍</mt-button>
                <mt-button plain size="large" type="danger" @click="goComment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>
<script>
import swiper from '../subcomponents/swiper.vue';
import numbox from '../subcomponents/goodInfo_numbox.vue'
export default {
    data(){
        return{
            id:this.$route.params.id,
            goodImg:{},
            goodInfo:{},
            ballFlag:false,
            selectCount:1
        }
    },
    created(){
        this.getlunbotuById();
        this.getGoodInfoById();
    },
    methods:{
        getlunbotuById(){
            this.$http.get('http://www.liulongbin.top:3005/api/getthumimages/'+this.id).then(result =>{
                if(result.body.status === 0){ 
                    //获取的数据不能直接复制 因为轮播图的组建中只能绑定img，但是现在的数据里面都是src
                    result.body.message.forEach(item => {
                        item.img = item.src;
                    });         
                    this.goodImg =result.body.message;
                }
            })
        },
        getGoodInfoById(){
            //当使用异步操作进行数据请求的时候 max被渲染到页面上的时候，可能数据还在获取中，想拿到数据，必须使用同步请求
            this.$http.get('http://www.liulongbin.top:3005/api/goods/getinfo/'+this.id).then(result =>{
                if(result.body.status === 0)              
                    this.goodInfo = result.body.message[0];
            })
        },
        goDesc(id){
            this.$router.push({name:'goodsdesc',params:{id}});
        },
        goComment(id){
            this.$router.push({name:'goodscomment',params:{id}});
        },
        addToShop(){
            this.ballFlag = !this.ballFlag;
            //将页面上需要的数据，放到store里面去
            //{id:商品id,count:,price:商品价格,selectedFlag:商品是否选中被结算},{}
            var goodCar = {id:this.id,count:this.selectCount,price:this.goodInfo.sell_price,selectFlag:true};
            //调用store里面的mutations
            this.$store.commit('addToCar',goodCar);
        },
        beforeEnter(el){
            el.style.transform = "translate(0,0)";          
        },
        enter(el,done){
            //getBoundingRect()
            const ballposition = this.$refs.ball.getBoundingClientRect();
            const badgeposition = document.getElementById('badge').getBoundingClientRect();
            const xD = badgeposition.left - ballposition.left;
            const yD = badgeposition.top - ballposition.top;
            el.offsetWidth;
            el.style.transform = `translate(${xD}px,${yD}px)`;
            el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
            done();
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag;
        },
        getSelectCount(count){
            this.selectCount = count;//子组件调用父组件的这个方法，并且将count拿到之后，给父组件的这个值
            console.log('父组件拿到了'+this.selectCount)
        }
    },
    components:{
        swiper,
        numbox
    }
}
</script>
<style lang="scss">
.goodinfo-container{
    background-color: #eee;
    overflow: hidden;

    .now-price{
    color: red;
    font-size: 16px;
    }
    .mui-card-footer{
        display: block;
        button{
            margin:5px auto;
        }
    }
    .ball{
        width:15px;
        height: 15px;
        border-radius: 50%;
        background-color: red;
        position: absolute;
        z-index:99;
        top:390px;
        left:146px;
    }
}

</style>